/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.enums.AdviceProfessional;
import br.com.foxline.clinicplus.enums.Gender;
import br.com.foxline.clinicplus.service.DoctorService;
import br.com.foxline.clinicplus.service.GroupService;
import br.com.foxline.clinicplus.service.SystemUserService;
import br.com.foxline.clinicplus.util.Caracter;
import br.com.foxline.clinicplus.util.Msg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerDoctor implements Serializable {

    @EJB
    DoctorService doctorService;
    @EJB
    SystemUserService systemUserService;
    @EJB
    GroupService groupService;
    private Doctor doctor;
    private Cbo cbo;
    private Country country;
    private Address address;
    private City city;
    private SystemUser systemUser;
    private String passwd;
    private String passwdConfirmation;

    @PostConstruct
    public void init() {
        doctor = new Doctor();
        cbo = new Cbo();
        country = new Country();
        address = new Address();
        city = new City();
        systemUser = new SystemUser();


    }

    public void create() {
        if (systemUser.getLogin() != null && !systemUser.getLogin().isEmpty()) {
            try {
                if (systemUserService.compareToPassword(passwdConfirmation, passwd)) {

                    if (systemUserService.checkUserLogin(systemUser)) {
                        systemUser.setName(doctor.getName());
                        systemUser.setCreateDat(new Date());
                        systemUser.setPassword(SystemUser.encryptPassword(passwd));
                        List<Groups> list = new ArrayList<Groups>();
                        list.add(groupService.getMedico());
                        systemUser.setGroups(list);

                        System.err.println(systemUser.getGroups());
                        systemUserService.save(systemUser);
                    } else {
                        Msg.messagemWarn("Login indisponível !");
                        return;
                    }

                } else {
                    Msg.messagemWarn("Senhas não conferem !");
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }

        try {
            address.setZipcode(Caracter.removecaracter(address.getZipcode()));
            doctor.setCellular(Caracter.removecaracter(doctor.getCellular()));
            doctor.setPhone(Caracter.removecaracter(doctor.getPhone()));
            doctor.setCbo(cbo);
            address.setCity(city);
            doctor.setAddress(address);
            doctor.setCountry(country);
            doctor.setCreateAt(new Date());
            doctor.setSystemUser(systemUser);
            doctorService.save(doctor);
            doctor = new Doctor();
            address = new Address();
            Msg.messagemInfo("Cadastrado com Sucesso !");
        } catch (Exception erro) {
            erro.printStackTrace();
            Msg.messagemInfo("Falha ao Cadastrar!");
        }
    }

    public List<SelectItem> getGender() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (Gender ms : Gender.values()) {
            genders.add(new SelectItem(ms, ms.getName()));
        }
        return genders;
    }

    public List<SelectItem> selectAdviceProfessional() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (AdviceProfessional ms : AdviceProfessional.values()) {
            genders.add(new SelectItem(ms, ms + ""));
        }
        return genders;
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public List<Doctor> autocomplete(String query) {
        return doctorService.autocomplete(query);
    }

    public Cbo getCbo() {
        return cbo;
    }

    public void setCbo(Cbo cbo) {
        this.cbo = cbo;
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    public String getPasswdConfirmation() {
        return passwdConfirmation;
    }

    public void setPasswdConfirmation(String passwdConfirmation) {
        this.passwdConfirmation = passwdConfirmation;
    }
}
